/**
 * 秒表工具
 * @description: 这里使用构造函数（Constructor）方法 
 * @author: HuiSir
 * @update: HuiSir(2019-6-13 10:34:43)
 */	

function Stopwatch(){
	let startTime , endTime , running , duration = 0
	
	//开始时间
	this.start = function () {
		if(running){
			throw new Error('秒表已启动');
		}
			
		running = true;
		startTime = new Date();
	};
	
	//结束时间
	this.stop = function() {
		if(!running){
			throw new Error('秒表暂未启动');
		}
		
		running = false;
		endTime = new Date();
		
		//计算秒数
		duration+= (endTime.getTime() - startTime.getTime()) / 1000;
	};
	
	//重置秒表
	this.reset = function() {
		startTime = null;
		endTime = null;
		running = false;
		duration = 0;
	};
	
	//只读属性暴露，将某变量转换为对象的属性
	Object.defineProperty(this,'duration',{
		get:function(){
			return duration;
		}
	})
}

//使用方法:
// var watch = new Stopwatch()
// watch.start()
// watch.stop()
// watch.duration;	//秒数	